var app = getApp(), u = app.require("utils/util.js"), a = app.require("utils/api.js"), o = app.require("utils/onfire.js"), h = app.require("utils/http.js"), n = app.require("utils/enums"),
  utilMd5 = app.require('utils/md5.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    orderId:null,
    integral:0,
    price:0,
    trade_no:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '等待微信支付',
    })
    this.data.userInfo = wx.getStorageSync('userInfo');
    console.log(options);
    this.data.orderId = options.orderId,
    this.data.integral = options.integral,
    this.data.price = options.price,
    this.data.trade_no = u._getNum(),
    this.wxPay();
  },
  // 发起微信支付
  wxPay:function(){
    var t = this;
    var data = {
      store_id: a.storeId,
      uid: t.data.userInfo.uid,
      order_no: t.data.orderId,
      pay_money: t.data.price,
      trade_no: t.data.trade_no
    };
    h.noLoadReq(a.wxRequestPay, data, 'POST', function (res) {
      if (res.code == 0) {
        var data = res.data;
        var timestamp = Date.parse(new Date());
        timestamp = (timestamp / 1000).toString();
        var signData = {
          appId: data.appid,
          nonceStr: data.nonce_str,
          package: 'prepay_id=' + data.prepay_id,
          timeStamp: timestamp
        };
        var paySign = t._generateSign(signData);
        wx.hideLoading();
        wx.requestPayment({
          'timeStamp': timestamp,
          'nonceStr': data.nonce_str,
          'package': 'prepay_id=' + data.prepay_id,
          'signType': 'MD5',
          'paySign': paySign,
          success: function (res) {
            console.log(res);
            var rest = res;
            // t.data.trade_no = res.data.trade_no,         
            u.success('支付成功', function () {
              // 支付成功的回调
              t.wxPaySuccess();
            });
          },
          fail: function (res) {
            console.log(res);
            u.error('支付失败'), wx.navigateBack();
          }
        });
      } else {
        u.toast(res.message);
      }
    });
  },
  // 支付成功后的回调
  wxPaySuccess: function (){
    var t = this;
    var data = {
      order_id: t.data.orderId,
      price: t.data.price * 100,
      payType: 4, //[0:线下支付|1:微信支付|2:支付宝|3:银联支付|4:小程序订单|5:第三方线下扫码]
      tradeNo: t.data.trade_no,
      score: t.data.integral,
      couponId:'',
      type: 1,//1 普通订单 2 商户订单 默认为1
    };
    h.noLoadReq(a.externalPaySuccess, data, 'POST', function (res) {
      if (res.code == 0) {
        wx.navigateTo({
          url: "/template/prescribe/prescribe?url=https://tc2.zmmedical.cn/home.htm"
        });
      } else {
        u.toast(res.message);
      }
    });
  },

  // 生成支付请求签名
  _generateSign: function (data) {
    var payKey = "l3L19WpE2biJbcYwrOmGiR4Ys7tHlFqG";
    var stringA = "appId=" + data.appId + "&nonceStr=" + data.nonceStr + "&package=" + data.package + "&signType=MD5&timeStamp=" + data.timeStamp + "&key=" + payKey;
    var sign = utilMd5.hexMD5(stringA).toUpperCase();
    return sign;
  },
})